package com.zcaxer.way.adapters;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zcaxer.way.R;
import com.zcaxer.way.books.Book;

import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {
    private List<Book> bookList;

    public BookAdapter(List<Book> bookList) {
        this.bookList = bookList;
    }

    @NonNull
    @Override
    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.book_item, viewGroup, false);
        BookViewHolder bookViewHolder = new BookViewHolder(view);
        return bookViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final BookViewHolder bookViewHolder, int i) {
        final Book book = bookList.get(i);
        bookViewHolder.bookName.setText(book.getName());
        bookViewHolder.bookLevel.setText("4级");
        bookViewHolder.bookGrade.setText(book.getGrade() + "星");
        bookViewHolder.bookEffect.setText("效果：" + book.getEffect());
        bookViewHolder.bookElement.setText(book.getElement());
        bookViewHolder.buttonUpgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bookViewHolder.bookLevel.setText("5级");
            }
        });
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public static class BookViewHolder extends RecyclerView.ViewHolder {
        TextView bookLevel;
        TextView bookName;
        TextView bookElement;
        TextView bookGrade;
        TextView bookEffect;
        Button buttonUpgrade;

        public BookViewHolder(View view) {
            super(view);
            bookEffect = view.findViewById(R.id.textView_bookEffect);
            bookElement = view.findViewById(R.id.textView_bookElement);
            bookGrade = view.findViewById(R.id.textView_bookGrade);
            bookLevel = view.findViewById(R.id.textView_bookLevel);
            bookName = view.findViewById(R.id.textView_bookName);
            buttonUpgrade = view.findViewById(R.id.button_bookUpgrade);
        }
    }
}
